package org.ichow.scenegame.view
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import org.ichow.scenegame.controller.MovieCommand;
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.events.SceneEvent;
	import org.ichow.scenegame.model.AssetsProxy;
	import org.ichow.scenegame.SceneGameFacade;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * ...
	 * @author M.ichow
	 */
	public class SceneMediator extends Mediator
	{
		public static const NAME : String = "SceneMediator";

		private var _loadMask:Sprite;
		
		
		public function SceneMediator( app : Object = null )
		{
			super( NAME , app );
		}

		override public function onRegister() : void
		{
			super.onRegister();
			//
			facade.registerCommand( SceneEvent.MOVIE_LOAD , MovieCommand );
		}

		override public function listNotificationInterests() : Array
		{
			return [ SceneEvent.INIT_GAME , SceneEvent.MOVIE_LOADING , SceneEvent.MOVIE_LOADED ];
		}

		override public function handleNotification( notification : INotification ) : void
		{
			switch ( notification.getName() )
			{
				case SceneEvent.INIT_GAME:
					sendNotification( SceneEvent.ADD_TO_SCENE , scene );
					sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.INIT );
					sendNotification( SceneEvent.MOVIE_LOAD , "start" );
					break;
				case SceneEvent.MOVIE_LOADING:
					// show loading
					if (!_loadMask)
					{
						var assets:AssetsProxy = facade.retrieveProxy(AssetsProxy.NAME) as AssetsProxy;
						var lodMc:MovieClip = assets.getSkin("preloader") as MovieClip;
						_loadMask = new Sprite();
						_loadMask.graphics.beginFill(0x333333 , .5);
						_loadMask.graphics.drawRect(0, 0, int(getValueForKey("width")) , int(getValueForKey("height")) );
						_loadMask.graphics.endFill();
						if (lodMc)
						{
							_loadMask.addChild(lodMc);
							lodMc.x = _loadMask.width / 2;
							lodMc.y = _loadMask.height / 2;
						}
					}
					scene.addChild(_loadMask);
					break;
				case SceneEvent.MOVIE_LOADED:
					// hide loading
					scene.removeChild(_loadMask);
					break;
			}
		}

		public function getValueForKey( v : String ) : Object
		{
			return scene.getValueForKey( v );
		}

		protected function get scene() : Main
		{
			return viewComponent as Main;
		}

	}

}